package com.qdevelop.control.action;

import java.util.Map;

import com.qdevelop.control.bean.QueryBean;
import com.qdevelop.control.bean.ResultBean;
import com.qdevelop.lang.IQdevelopConstant;
import com.qdevelop.model.service.database.DBFactory;
import com.qdevelop.model.utils.QdevelopUtils;
import com.qdevelop.model.utils.UtilsFactory;

@SuppressWarnings("serial")
public class SecurityAction extends QDevelopBaseAction{
	private String key = "SecurityAction.UserNameList";
	String userName,msg;
	
	
	@SuppressWarnings("unchecked")
	public String execute() throws Exception {
		Map tmp = this.getParamMap();
		tmp.remove("forward");
		if(!this.isRepeatSubmit(tmp) && tmp!=null && tmp.size()>0){
			QueryBean qb = QdevelopUtils.getInstance().getQueryBean(tmp);
			int cn = DBFactory.getInstance().updateBySQL(qb);
			if(cn>0) initCheckUserCache();
		}
		return SUCCESS;
	}
	
	public String checkSingleUser()throws Exception {
		if(!UtilsFactory.CacheUtils().isCached(key,IQdevelopConstant._Result_Cache_Tactics_KEY)){
			initCheckUserCache();
		}
		String userNames = (String)UtilsFactory.CacheUtils().getCache(key,IQdevelopConstant._Result_Cache_Tactics_KEY);
		StringBuffer sb = new StringBuffer();
		sb.append("#").append(userName).append("#");
		if(userNames.indexOf(sb.toString())>-1){
			msg = "false";
		}else{
			msg = "true";
		}
		return SUCCESS;
	}
	
	@SuppressWarnings("unchecked")
	public void initCheckUserCache(){
		StringBuffer sb = new StringBuffer();
		sb.append("#");
		QueryBean qb = QdevelopUtils.getInstance().getQueryBean("quaryUsers");
		qb.setMaxNum(-1);
		qb.setPage(-1);
		qb.setCacheConfig("directdata");
		ResultBean rb = DBFactory.getInstance().getQueryResult(qb);
		for(int i=0;i<rb.resultList.size();i++){
			Map tmp = rb.getResultMap(i);
			sb.append(tmp.get("LOGINNAME")).append("#");
		}
		System.out.println("initCheckUserCache ==> "+sb.toString());
		UtilsFactory.CacheUtils().setCache(key, sb.toString(),IQdevelopConstant._Result_Cache_Tactics_KEY);
	}

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}
	
}
